﻿using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{


		public static int playerLives ;
		public static int playerHealth;
		public static int planetHealth;
		public GameObject playerShip;
		public Transform spawnLocation;
		public static bool playerAlive;
		private GameObject[] ships;
		public static bool showMenu;
		public AudioClip explosionSound;
		public int missionQty;
		public static int waveCount;
		public static int missionDifficulty;
		public int difficultyModifier;


		// Added variables for new ManagedSpawner
		bool spawnedMission;
		int missionCounter;

		// Use this for initialization
		void Start ()
		{
				playerAlive = false;
				playerLives = 3;
				playerHealth = 500;
				planetHealth = 1500;
				showMenu = false;
				missionCounter = 1;
				//missionDifficulty = 5;
				difficultyModifier = 3;
				spawnedMission = false;
				waveCount = missionCounter;

		if (Application.loadedLevelName == "Level 1") {
			missionDifficulty = 5;	
		}
		if (Application.loadedLevelName == "Level 2") {
			missionDifficulty = 5 + (difficultyModifier * 5);	
		}
				
		}
	
		// Update is called once per frame
		void Update ()
		{
				DestroyPlayer ();
				SpawnPlayer ();
				GameOver ();
				ToggleMenu ();
				MissionManager ();
		}
		void UpdateWaveCount()
		{
		waveCount = missionCounter;
		}
		void GameOver ()
		{
				if (playerLives <= 0 || planetHealth <= 0) {
						Debug.Log ("Game OVER");
						Application.LoadLevel ("Game Over");	
				}
		}

		void SpawnPlayer ()
		{
				if (playerLives > 0 && playerAlive == false) {
						ships = GameObject.FindGameObjectsWithTag ("Player");

						for (var i = 0; i< ships.Length; i++) {
								Destroy (ships [i]);
						}
						Instantiate (playerShip, spawnLocation.position, spawnLocation.rotation);
						playerAlive = true;
						playerHealth = 500;
				}
		}

		void DestroyPlayer ()
		{

				if (playerHealth <= 0) {
						ships = GameObject.FindGameObjectsWithTag ("Player");
						audio.PlayOneShot (explosionSound);
						for (var i = 0; i< ships.Length; i++) {
								Destroy (ships [i]);
						}
						playerAlive = false;
						playerLives--;//decrease the player life counter by 1		
				}
		}

		void ToggleMenu ()
		{
				if (Input.GetKeyDown (KeyCode.Escape)) {
						if (!showMenu) {
								showMenu = true;
						} else if (showMenu) {
								showMenu = false;
						}
				}
		}

		void MissionManager ()
		{
				GameObject[] enemyList;
				ManagedSpawner spawner = GetComponent<ManagedSpawner> ();

				// Setup initialization for spawning a mission.
				// Set to false when we're ready to start a new mission.
				// Set to true after values initialized so we don't reset each update.
				if (!spawnedMission) {
						Debug.Log ("Selecting Enemies");
						spawner.SelectEnemies (missionDifficulty);
						Debug.Log ("Setting up Wave");
						spawner.SetupWave ();
						Debug.Log ("Spawning Mission");
						spawner.SpawnMission ();
						spawnedMission = true;
				}

				//add all spawned enemies into an array
				enemyList = GameObject.FindGameObjectsWithTag ("Enemy"); 

				//Check to see if all enemies have been killed thus completing mission
				if (enemyList.Length <= 0 && spawner.allSpawned == true) 
				{
						missionCounter++;
						UpdateWaveCount();
						Debug.Log ("Increased MissionCounter to " + missionCounter);

						//Make sure our current mission count doesnt exceed our max mission quantity for the level and if so end the level
						if (missionCounter >= missionQty+1) 
						{
								//Level Complete
								missionCounter = 0;
								Application.LoadLevel ("Level 2");	//Place holder till win screen is created
								
						}
						//If it doesnt increase the mission difficulty and spawn a new mission
						else if (missionCounter < missionQty) {
								missionDifficulty += difficultyModifier;
						}
						spawnedMission = false;
						Debug.Log ("Set spawnedMission to false.");
				}
		}




}
